
///
/// This is the Server class
///

#ifndef SERVER_H
#define SERVER_H

#include "Handler.h"
#include <map>
#include <utility>
#include <string>


#define BACKLOG 10

typedef std::map<int, Handler > SocketToHandlerMapping;
typedef std::map<int, int> OriginToClientFDMapping;

class Server
{

public:


	int Initialize(const char* ip, const char* portNo);

	// we'll see about other constructors

	int Execute();

	static Server* GetInstance() { if(mServer == NULL) mServer = new Server; return mServer; }

	static void Destroy() { if(mServer) delete mServer; }

	int MapOriginToClientFD(int originFD, int clientFD);
	
	int Die(int clientFD, int originFD);

private:

	// private constructor for singleton
	Server() {}

	void KillClient(int sockFD);
	void UpdateMaxFd();

	int mMainFD;
	
	fd_set mConnectedFDS;
	int mMaxFD;

	char mMsgBuf[MSG_BUF_SIZE];

	SocketToHandlerMapping mClientMap;
	OriginToClientFDMapping mOriginToClientMap;

	static Server* mServer;

};

#endif //SERVER_H
